package com.tiancikeji.zaoke.app.common.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tiancikeji.zaoke.app.common.constant.DataDict;

public class WebContext extends HashMap<String, Object> implements Map<String, Object> {
    private static final long serialVersionUID = 1L;

    private static ThreadLocal<HashMap<String, Object>> cache = new ThreadLocal<HashMap<String, Object>>() {
        @Override
        protected HashMap<String, Object> initialValue() {
            return new HashMap<String, Object>();
        }
    };

    private static WebContext instance = new WebContext();

    public synchronized static WebContext getInstance() {
        return instance;
    }

    @Override
    public void clear() {
        cache.remove();
    }

    @Override
    public boolean containsKey(Object key) {
        return cache.get().containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return cache.get().containsValue(value);
    }

    @Override
    public Set<java.util.Map.Entry<String, Object>> entrySet() {
        return cache.get().entrySet();
    }

    @Override
    public Object get(Object key) {
        return cache.get().get(key);
    }

    @Override
    public boolean isEmpty() {
        return cache.get().isEmpty();
    }

    @Override
    public Set<String> keySet() {
        return cache.get().keySet();
    }

    @Override
    public Object put(String key, Object value) {
        return cache.get().put(key, value);
    }

    @Override
    public void putAll(Map<? extends String, ? extends Object> m) {
        cache.get().putAll(m);
    }

    @Override
    public Object remove(Object key) {
        return cache.get().remove(key);
    }

    @Override
    public int size() {
        return cache.get().size();
    }

    @Override
    public Collection<Object> values() {
        return cache.get().values();
    }

    public HttpServletRequest getRequest() {
        return (HttpServletRequest) get(DataDict.REQUEST.toString());
    }

    public HttpServletResponse getResponse() {
        return (HttpServletResponse) get(DataDict.RESPONSE.toString());
    }

    public static ThreadLocal<HashMap<String, Object>> getCache() {
        return cache;
    }

}
